package com.victualler.core.base.controller.impl;

import com.victualler.core.base.controller.BaseRestController;
import com.victualler.core.base.dao.BaseDao;
import com.victualler.core.base.repository.BaseRepository;
import com.victualler.core.base.repository.IBaseQueryRepository;
import com.victualler.core.base.service.BaseService;
import com.victualler.core.common.dto.QueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 18:36 2018/4/5
 * @Discription:
 */
public abstract class BaseRestControllerImpl<E extends BaseDao,R extends BaseRepository<E,Long>,QR extends IBaseQueryRepository<E,Long>,S extends BaseService<E,R,QR>> implements BaseRestController<E,R,QR,S> {
    @Autowired
    protected  S baseService;
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public E save(@RequestBody E dao){
        System.out.println(dao.getClass()+" Contoller excute: save");
        baseService.save(dao);
        return dao;
    }

    @RequestMapping("findAll")
    @ResponseBody
    public List<E> findAll(){
        System.out.println(" Contoller excute: findAll");
        return  baseService.getAll();
    }

    @RequestMapping("find/{id}")
    @ResponseBody
    public E find(@PathVariable("id") Long id){
        System.out.println(" Contoller excute: find "+id);
        return baseService.getById(id);
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete(@RequestBody Long id){
        System.out.println(" Contoller excute: delete :"+id);
        baseService.deleteById(id);
    }


    /**
     * 获取页面长度
     * @return
     */
    @RequestMapping(value = "getPageLength")
    @ResponseBody
    public Long getPageLength(){
        System.out.println(" Contoller excute: getPageLength :");
        return baseService.countAllPage();
    }


    /**
     * 获取厂商信息根据页号
     * @param pageNo
     * @return
     */
    @RequestMapping("getByPage")
    @Override
    public List<E> getByPageNo(@RequestBody int pageNo) {
       return this.baseService.getAllByPage(pageNo,false,"state","createDate");
    }
    
    
    /**
     * 根据查询条件获取结果
     * @param queryDtoList
     * @return
     */
    @RequestMapping("findByCondition")
    @Override
    public List<E>  findByCondition(@RequestBody List<QueryDto> queryDtoList){
        return this.baseService.getByQueryList(queryDtoList);
    }
    /**
     * 根据查询条件获取分页结果
     * @param queryDtoList
     * @param pageNo
     * @return
     */
    @RequestMapping("findByCondition/{pageNo}")
    @Override
    public List<E>  findByCondition(@RequestBody List<QueryDto> queryDtoList,@PathVariable("pageNo") int pageNo){
       return this.baseService.getByQueryList(queryDtoList,pageNo);
    }

    /**
     * 根据查询条件获取分页信息
     * @param queryDtoList
     * @return
     */
    @RequestMapping(value = "getPageLengthByParam")
    @ResponseBody
    public Long getPageLength(@RequestBody List<QueryDto> queryDtoList){
        System.out.println(" Contoller excute: getPageLength queryDtoList:");
        return baseService.countAllPage(queryDtoList);
    }


}
